/**
 *
 * @author MCignarale
 */
package it.azienda.framework.utilita;
/*
import java.util.zip.*;
import java.io.*;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import org.apache.tools.ant.types.ZipFileSet;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
*/
public class JZipDir {

    // nomeZip   --> percorso dello zip da salvare
    // nomeFile  --> Percorso del file da zippare
    // tipoZip   --> 0: salva su disco   1:restituisci byte
    /*
    public JZipDir(String nomeZip, String nomeFile, int tipoZip) {
        
        try {
        if(tipoZip==0){
        salvaZip(nomeZip, nomeFile);
        }else{
        caricaZip(nomeZip, nomeFile);
        }
        } catch (Exception e) {
        e.printStackTrace();
        }finally{
        if(tipoZip==1){
        File fileZippato = new File(nomeZip);
        fileZippato.delete();
        File zip = new File(nomeFile);
        zip.delete();
        }
        }
         
    }

    public void salvaZip(String nomeZip, String nomeFile) throws java.io.IOException {
        
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(nomeZip));
        int n = 0;
        
        byte[] buff = new byte[2156];
        File zip = new File(nomeFile);
        FileInputStream fisd = new FileInputStream(zip);
        out.putNextEntry(new ZipEntry(zip.getPath()));
        while ((n = fisd.read(buff, 0, buff.length)) != -1)
        out.write(buff, 0, n);
        fisd.close();
        out.close();
         
    }

    public void caricaZip(String nomeZip, String nomeFile) throws java.io.IOException {
        
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(nomeZip));
        int n = 0;        
        byte[] buff = new byte[2156];
        File zip = new File(nomeFile);
        FileInputStream fisd = new FileInputStream(zip);
        out.putNextEntry(new ZipEntry(zip.getPath()));
        while ((n = fisd.read(buff, 0, buff.length)) != -1)
        out.write(buff, 0, n);
        fisd.close();
        out.close();
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileInputStream fisd2 = new FileInputStream(nomeZip);
        while ((n = fisd2.read(buff, 0, buff.length)) != -1)
        baos.write(buff, 0, n);
        fisd2.close();
        
        // Get the compressed data
        byte[] bytes = baos.toByteArray();
        Utilita.getRequest().setAttribute("byte",bytes);
        Utilita.getRequest().setAttribute("file",nomeFile.subSequence(3,nomeFile.length())+".zip");
        String url2 = "/visualizzaZip.jsf";
        FacesContext ctx = FacesContext.getCurrentInstance();
        UIViewRoot view = ctx.getApplication().getViewHandler().createView(ctx,url2);
        view.setViewId(url2);
        ctx.setViewRoot(view);
        ctx.renderResponse();        
         
    }
     */
}
